<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 12.2.&nbsp; Manipulating Table Data</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-12-SECT-2"></a>
<h3 id="631450-812" class="docSection1Title">12.2. Manipulating Table Data</h3>
<p class="docText">Since you've practiced executing a few SQL commands that manipulate database objects, you're ready to work with the data in your tables.</p>
<a name="learnphpmysql-CHP-12-SECT-2.1"></a>
<h4 id="title-IDAMKOWC" class="docSection2Title">12.2.1. Adding Data</h4>
<a name="IDX-CHP-12-0587"></a> 
<a name="IDX-CHP-12-0588"></a> 
<a name="IDX-CHP-12-0589"></a> 
<a name="IDX-CHP-12-0590"></a> 
<a name="IDX-CHP-12-0591"></a> 

<p class="docText">Naturally, you'll need to add rows to your tables. To add a purchase to your new <tt>purchases</tt> table, you'll use an <tt>INSERT</tt> statement in your query. <a class="docLink" href="#learnphpmysql-CHP-12-EX-3">Example 12-3</a> shows how this is done.</p>
<a name="learnphpmysql-CHP-12-EX-3"></a><h5 id="title-IDAWLOWC" class="docExampleTitle">Example 12-3. Using a predefined INSERT statement in insert.php</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
}
$query = "<b>INSERT</b> <B>INTO `purchases` VALUES (NULL,'mdavis',2,NULL)</b>";
$result = $connection-&gt;query($query);
if (DB::isError($result)){
die("Could not query the database: &lt;br /&gt;". $query." ".DB::errorMessage($result));
}
echo "Inserted successfully!";
$connection-&gt;disconnect();
?&gt;
</pre><br>

</td></TR></table></P>
<p class="docText">When you call up <span class="docEmphasis">insert.php</span>, in your browser, you get:</p>
<pre>
Inserted successfully!
</pre><BR>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-12-FIG-3">Figure 12-3</a> shows that the new row made it to the database by selecting all rows from <tt>purchases</tt>.</p>
<a name="learnphpmysql-CHP-12-FIG-3"></a><p><center>
<h5 class="docFigureTitle">Figure 12-3. Validating that our new row is in the database</H5>
<img border="0" alt="" id="195131084204" width="549" height="115" SRC="images/learnphpmysql_1203.jpg">
</center></p><BR>


</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
